<?php

class sendurldata {
	
	//调用接口
	public function send($url,$data,$method) {
		if(strtoupper($method)=='POST') {
			$data_string = json_encode($data);
			$result = $this->http_post_data($url,$data_string);
			return $result;
		}
		
		if(strtoupper($method)=='GET') {
			$result = $this->http_get_data($url,$data);
			return $result;
		}
	}
	
	//数据用JSON类型POST方式发送
	public function http_post_data($url, $data_string) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/json; charset=utf-8',
			'Content-Length: ' . strlen($data_string))
		);
		ob_start();
		curl_exec($ch);
		$return_content = ob_get_contents();
		ob_end_clean();
		curl_close($ch);
		
		return $return_content;
	}
	
	//数据用JSON类型GET方式发送
	public function http_get_data($url, $data_string) {
		//重组URL
		$i = 0;
		$url.= "?";
		foreach($data_string as $k=>$v) {
			if($i==0) {
				$url.= $k."=".$v;
			}else{
				$url.= "&".$k."=".$v;
			}
			$i++;
		}
		
		$ch = curl_init();
 		
		$timeout = 5;
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		$return_content = curl_exec($ch);
		curl_close($ch);
		 
		return $return_content;
	}
}

?>